<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2018/5/17
 * Time: 15:09
 */

namespace app\index\controller\sunfc;


use app\index\controller\Base;
use think\Db;

class Driver extends Base
{
    //司机首页
    public function Index(){
        $res= Db::name('dd_users')->field('type')->where('uid',$this->uid)->where('type',2)->select();
        if ($res){
            if(input('order_id')){
                //拿到行程中数据
                //此处拿的是乘客users表的电话
                $trip_data=Db::name('dd_publish_orders')->alias('o')
                    ->join('iw_dd_users u','u.uid=o.passenger_id')
                    ->join('iw_dd_publish p','o.publish_id=p.id')
                    ->field('u.headimgurl,u.nickname,u.trust_rank,
                        u.car_info,u.tel,u.sex,p.depart_time,o.id,
                        o.number,p.begin_place,p.region,
                        p.begin_detail,p.end_place,p.end_detail,
                        p.status')
                    ->where('o.id', input('order_id'))
                    ->find();
            }else{
                //拿到行程中数据
                $trip_data=Db::name('dd_publish_orders')->alias('o')
                    ->join('iw_dd_users u','u.uid=o.passenger_id')
                    ->join('iw_dd_publish p','o.publish_id=p.id')
                    ->field('u.headimgurl,u.nickname,u.trust_rank,
                        u.car_info,u.tel,u.sex,p.depart_time,o.id,
                        o.number,p.begin_place,
                        p.begin_detail,p.end_place,p.end_detail,
                        p.status,p.region')
                    ->where('o.driver_id',$this->uid)
                    ->where('o.status',['=',0],['=',1],'or')
                    ->find();
            }
            /*   //判断是否绑定手机号
               $users_tel=Db::name('dd_users')->where('uid',$this->uid)->field('tel')->find();
               $this->assign('users_tel',$users_tel['tel']);*/

            //拿到用户类型
            $type =Db::name('dd_users')
                ->field("type,uid")
                ->where('uid', $this->uid)
                ->find();
            $this->assign('type',$type);

            //拿到发布中数据
            $publish_data=Db::name('dd_publish')
                ->where('uid', $this->uid)
                ->where('is_del',0)
                ->where('status',0)
                ->where('type',2)
                ->find();
            if(isset($publish_data)){
                //部分数据进行处理
                if($publish_data['depart_time']>strtotime(date('Y-m-d',strtotime('day')))){
                    $publish_data['depart_time']="今天".date('H:i',$publish_data['depart_time']);
                }
                else{
                    $publish_data['depart_time']= date('m-d H:i',$publish_data['depart_time']);
                }
            }else{
                $publish_data="";
            }
            $this->assign('publish_data',$publish_data);

            if(isset($trip_data)){
                //部分数据进行处理
                if($trip_data['depart_time']>strtotime(date('Y-m-d',strtotime('day')))){
                    $trip_data['depart_time']="今天".date('H:i',$trip_data['depart_time']);
                }
                else{
                    $trip_data['depart_time']= date('m-d H:i',$trip_data['depart_time']);
                }
            }else{
                $trip_data="";
            }

            $this->assign('trip_data',$trip_data);

            //拿到附近乘客信息
            $car_incity = [];
            $car_incity1=Db::name('dd_publish')
                ->field('end_detail,count(end_detail) as co')
                ->where('type',1)
                ->where('status',0)
                ->where('region',0)
                ->group('end_detail')
                ->select();
            $sum1=0;
            foreach ($car_incity1 as $k=>$sum){
                $sum1+=$sum['co'];
                if($k<6){
                    $car_incity[$k] = $sum;
                }
            }
            $this->assign('sum1',$sum1);
            $this->assign('car_incity',$car_incity);

            //        拿到跨城乘客信息
            $car_outcity = [];
            $car_outcity1=Db::name('dd_publish')
                ->field('end_place,count(end_place) as co')
                ->where('type',1)
                ->where('status',0)
                ->where('region',1)
                ->group('end_place')
                ->select();
            $sum2=0;
            foreach ($car_outcity1 as $k=>$sum){
                $sum2+=$sum['co'];
                if($k<6){
                    $car_outcity[$k] = $sum;
                }
            }
            $this->assign('sum2',$sum2);
            $this->assign('car_outcity',$car_outcity);

            return $this->fetch();
        }else{
//           跳转到成为车主页面
            return $this->redirect('cat_owner');
        }
    }

    //司机发布
    public function add_publish(){
        if (\request()->isGet()){
            $data=input();
            //如果是取消成功跳转到发布（重新发布）
            if (input('publish_id')){
                $data=Db::name('dd_publish')
                    ->field('begin_detail,begin_place,end_detail,end_place,region,lonlat_begin,lonlat_end')
                    ->where('id',input('publish_id'))
                    ->find();
                $this->assign('data',$data);
                return $this->fetch();
            }
            else{
                $this->assign('data',$data);
                return $this->fetch();
            }
        }
        else{
            $this->redirect(url('index'));
        }
    }

    //等待接单
    public function wait_order(){
        if (request()->isPost()){
            $res=Db::name('dd_users')
                ->field('tel')
                ->where('uid',$this->uid)
                ->find();
            $arr = explode("-", input('number_info'));
            $ck=['adult'=>$arr[0],'children'=>$arr[1],'infant'=>$arr[2]];
            $data=[
                'uid'=>$this->uid,
                'depart_time'=>strtotime(input('depart_time')),
                'content'=>input('content'),
                'type'=>2,
                'status'=>0,
                'begin_detail'=>input('begin_detail'),
                'begin_place'=>input('begin_place'),
                'end_detail'=>input('end_detail'),
                'end_place'=>input('end_place'),
                'number_info'=>$ck,
                'phone'=>$res['tel'],
                'lonlat_begin'=>input('lonlat_begin'),
                'lonlat_end'=>input('lonlat_end'),
                'region'=>input('region'),
            ];
            $driver=new \app\index\model\driver\Driver();
            $re = $driver->insert_data($data);
            if($re){

                if($data['region']==1){
                    $order=[
                        'begin_detail'=>input('begin_detail'),
                        'begin_place'=>input('begin_place'),
                        'end_detail'=>input('end_detail'),
                        'end_place'=>input('end_place')
                    ];

                }else{
                    $order=[
                        'begin_detail'=>input('begin_detail'),
                        'begin_place'=>'',
                        'end_detail'=>input('end_detail'),
                        'end_place'=>'',
                    ];
                }
                $this->publish_ok($re);
                $this->assign('publish_id',$re);
                $this->assign('order',$order);
                return $this->fetch();
            }else{
                $this->redirect('add_publish');

            }
        }
        else{
            if(input('publish_id')){
                $order1= Db::name('dd_publish')
                    ->where('id',input('publish_id'))
                    ->find();
                if(isset($order1)){
                    $this->assign('publish_id',$order1['id']);
                    $this->assign('order',$order1);
                    return $this->fetch();
                }else{
                    $this->redirect(url("index"));
                }
            }else{
                $this->redirect(url("index"));
            }


        }
    }

    //取消订单
    public function cancel_order(){
        if(input('publish_id')){
            $this->assign('publish_id',input('publish_id'));
            return $this->fetch();
        }else{
            $this->redirect(url("index"));
        }

    }

    //成功取消订单
    public function cancel_success(){
        if(input('publish_id')){
            $res=Db::name('dd_publish')->where('id',input('publish_id'))->update(['status'=>3]);
            if ($res){
                $this->publish_cancle(input('publish_id'));
                $this->assign('publish_id',input('publish_id'));
                return $this->fetch();
            }else{
                $this->error('取消失败');
            }
        }else{
            if (input('publish')){
                $this->assign('publish_id',input('publish'));
                return $this->fetch();
            }else{
                $this->redirect(url('index'));
            }
        }
    }

//？？行程中订单
    public function trip()
    {
        if (input('order_id')) {
            $trip_data = Db::name('dd_publish_orders')->alias('o')
                ->join('iw_dd_users u', 'u.uid=o.passenger_id')
                ->join('iw_dd_publish p', 'o.publish_id=p.id')
                ->field('u.headimgurl,u.nickname,u.trust_rank,
                                u.car_info,u.tel,u.sex,p.depart_time,
                                o.number,p.begin_place,o.id,o.driver_id,
                                p.begin_detail,p.end_place,p.end_detail,
                                p.region,o.status,p.lonlat_begin,p.lonlat_end')
                ->where('o.id', input('order_id'))
                ->find();
            if($trip_data['driver_id']!=$this->uid){
                $trip_data['type'] = 1;
            }else{
                $trip_data['type'] = 2;
            }
            if ($trip_data['depart_time'] > strtotime(date('Y-m-d', strtotime('day')))) {
                $trip_data['depart_time'] = "今天" . date('H:i', $trip_data['depart_time']);
            } else {
                $trip_data['depart_time'] = date('m-d H:i', $trip_data['depart_time']);
            }
            $trip_data['lonlat_begin'] = explode(',', $trip_data['lonlat_begin']);
            $trip_data['lonlat_end'] = explode(',', $trip_data['lonlat_end']);
            $this->assign('trip_data', $trip_data);
            return $this->fetch();
        } else {
            $this->redirect(url("index"));
        }

    }

    //市内乘客
    public function car_incity(){
        if(input('city')){
            //此处拿的是发布的电话
            $car_incity=Db::name('dd_publish')->alias('p')
                ->join('iw_dd_users u','u.uid=p.uid')
                ->field('u.headimgurl,u.nickname,u.trust_rank,
                        u.car_info,p.phone,u.sex,p.id,
                        p.begin_place,p.begin_detail,p.end_place,
                        p.end_detail,p.status,p.depart_time,
                        p.number')
                ->where('p.is_del',0)
                ->where('p.status',0)
                ->where('p.region',0)
                ->where('p.type',1)
                ->where('p.end_detail',input('city'))
                ->select();

        }else{
            $car_incity=Db::name('dd_publish')->alias('p')
                ->join('iw_dd_users u','u.uid=p.uid')
                ->field('u.headimgurl,u.nickname,u.trust_rank,
                        u.car_info,p.phone,u.sex,p.id,
                        p.begin_place,p.begin_detail,p.end_place,
                        p.end_detail,p.status,p.depart_time,
                        p.number')
                ->where('p.is_del',0)
                ->where('p.status',0)
                ->where('p.region',0)
                ->where('p.type',1)
                ->select();

        }
        foreach ($car_incity as $k=>$v){

            if($v['depart_time']>strtotime(date('Y-m-d',strtotime('day')))){
                $car_incity[$k]['depart_time']="今天".date('H:i',$v['depart_time']);
            }
            else{
                $car_incity[$k]['depart_time']= date('m-d H:i',$v['depart_time']);
            }
        }
        $this->assign('count',count($car_incity));
        $this->assign('car_incity',$car_incity);
        return $this->fetch();
    }

    //跨城乘客
    public function car_outcity(){
        if(input('city')){
            $car_outcity=Db::name('dd_publish')->alias('p')
                ->join('iw_dd_users u','u.uid=p.uid')
                ->field('u.headimgurl,u.nickname,u.trust_rank,u.car_info,p.phone,u.sex,
                            p.id,p.begin_place,p.begin_detail,p.end_place,p.end_detail,
                            p.status,p.depart_time,p.number')
                ->where('p.is_del',0)
                ->where('p.status',0)
                ->where('p.region',1)
                ->where('p.type',1)
                ->where('p.end_place',input('city'))
                ->select();
        }else{
            $car_outcity=Db::name('dd_publish')->alias('p')
                ->join('iw_dd_users u','u.uid=p.uid')
                ->field('u.headimgurl,u.nickname,u.trust_rank,u.car_info,p.phone,u.sex,
                            p.id,p.begin_place,p.begin_detail,p.end_place,p.end_detail,
                            p.status,p.depart_time,p.number')
                ->where('p.is_del',0)
                ->where('p.status',0)
                ->where('p.region',1)
                ->where('p.type',1)
                ->select();
        }


        foreach ($car_outcity as $k=>$v){

            if($v['depart_time']>strtotime(date('Y-m-d',strtotime('day')))){
                $car_outcity[$k]['depart_time']="今天".date('H:i',$v['depart_time']);
            }
            else{
                $car_outcity[$k]['depart_time']= date('m-d H:i',$v['depart_time']);
            }
        }
        $this->assign('count',count($car_outcity));
        $this->assign('car_outcity',$car_outcity);
        return $this->fetch();
    }

    //接单
    public function order(){
        if(input('publish_id')){
            //如果信息不完整
            $driver_tel=Db::name('dd_users')->field('tel')->where('uid',$this->uid)->find();
            //如果该司机order表有预约中订单
            $driver_ordering=Db::name('dd_publish_orders')->where('driver_id',$this->uid)->where('status',0)->find();
            //如果该乘客publish信息状态不为0
            $passenger_publish_status = Db::name('dd_publish')->field('status')->where('id',input('publish_id'))->find();
            //不能接自己单
            $dd_publish_uid= Db::name('dd_publish')->field('uid')->where('id',input('publish_id'))->find();
            if(!$driver_tel){
                $this->error('信息不完整','sunfc.users/authuser');
            }
            if($driver_ordering){
                $this->error('有正在进行的订单');
            }
            if($passenger_publish_status['status']!=0){
                $this->error('慢了一步呦');
            }
            if($dd_publish_uid['uid']==$this->uid){
                $this->error('不可接自己发布的行程呦');
            }
            //开启事务
            Db::startTrans();
            try{
                //更新该乘客publish信息数据
                Db::name('dd_publish')->where('id',input('publish_id'))->update(['status'=>1]);
                //插入order数据
                $passenger_publish_msg = Db::name('dd_publish')->field('number,uid')->where('id',input('publish_id'))->find();
                $data = [
                    'order_id'=>date('YmdHis') . rand(1000,9999),
                    'passenger_id'=>$passenger_publish_msg['uid'],
                    'driver_id'=>$this->uid,
                    'publish_id'=>input('publish_id'),
                    'number'=>$passenger_publish_msg['number'],
                    'money'=>$passenger_publish_msg['number']*5,
                    'status'=>0,
                    'create_time'=>time()
                ];
                $get_order_id = Db::name('dd_publish_orders')->insertGetId($data);
                //给乘客发送接单成功信息
                $get_order=Db::name('dd_publish_orders')->where('id',$get_order_id)->field('passenger_id,publish_id')->find();
                $get_user= Db::name('dd_users')->where('uid',$get_order['passenger_id'])->field('openid,realname')->find();
                $get_publish=Db::name('dd_publish')->where('id',$get_order['publish_id'])->find();
                if ($get_publish){
                    $weixin = new  \app\index\controller\Weixin();
                    $openid         =$get_user['openid'];

                    $re=$this->getWxTemplate();
                    $res=json_decode($re, true);
                    $templete_id    = $res['yuyue_ok'];
                    $url            = Request()->domain().'/index/sunfc.passenger/index';
                    $data_arr['user_name']                  = ['value'=>"亲爱的".$get_user['realname']."，您的订单已被司机成功接单",'color'=>'#ff8a5a'];
                    $data_arr['begin_place']                = ['value'=>$get_publish['begin_place'].$get_publish['begin_detail'],'color'=>'#1491cb'];
                    $data_arr['end_place']                  = ['value'=>$get_publish['end_place'].$get_publish['end_detail'],'color'=>'#1491cb'];
                    $data_arr['depart_time']                = ['value'=>date("Y-m-d H:i",$get_publish['depart_time']),'color'=>'#1491cb'];
                    $data_arr['number']                     = ['value'=>$get_publish['number'],'color'=>'#1491cb'];
                    $data_arr['content']                    = ['value'=>$get_publish['content'],'color'=>'#1491cb'];

                    $re = $weixin->sentMsg($openid , $templete_id , $url ,$data_arr);
                    if($re['errcode'] != 0){
                        return $this->error("错误代码".$re['errcode']);
                    }
                }
                Db::commit();
                return $get_order_id;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }

        }
        else{
            $this->redirect('index');
        }
    }


    //取消还未上车订单
    public function cancel_ordering(){
        if(input('order_id')){

            Db::startTrans();
            try{
                //更新订单状态
                Db::name('dd_publish_orders')
                    ->where('id',input('order_id'))
                    ->update(['status'=>-1]);

                //更新发布状态
                $type = Db::name('dd_publish_orders')
                    ->join('iw_dd_publish','iw_dd_publish.id=iw_dd_publish_orders.publish_id')
                    ->field('iw_dd_publish.type,iw_dd_publish.id')
                    ->where('iw_dd_publish_orders.id',input('order_id'))
                    ->find();

                //如果是司机发布的
                if($type["type"]==2){
                   Db::name('dd_publish')
                        ->where('id',$type['id'])
                        ->update(['status'=>3]);
                }else{
                    Db::name('dd_publish')
                        ->where('id',$type['id'])
                        ->update(['status'=>0]);
                }

                //给乘客发送接单取消信息
                $get_order=Db::name('dd_publish_orders')->where('id',input('order_id'))->field('publish_id,passenger_id')->find();
                $get_user= Db::name('dd_users')->where('uid',$get_order['passenger_id'])->field('openid,realname')->find();
                $get_publish=Db::name('dd_publish')->where('id',$get_order['publish_id'])->find();
                if ($get_publish){
                    $weixin = new  \app\index\controller\Weixin();
                    $openid         = $get_user['openid'];
                    $re=$this->getWxTemplate();
                    $res=json_decode($re, true);
                    $templete_id    = $res['cancel_publish'];
                    $url            = Request()->domain().'/index/sunfc.passenger/index';

                    $data_arr['user_name']                  = ['value'=>"亲爱的".$get_user['realname']."，您的订单已被司机取消",'color'=>'#ff8a5a'];
                    $data_arr['begin_place']                = ['value'=>$get_publish['begin_place'].$get_publish['begin_detail'],'color'=>'#1491cb'];
                    $data_arr['end_place']                 = ['value'=>$get_publish['end_place'].$get_publish['end_detail'],'color'=>'#1491cb'];
                    $data_arr['cancel_time']                = ['value'=>date("Y-m-d H:i:s"),'color'=>'#1491cb'];

                    $re = $weixin->sentMsg($openid , $templete_id , $url ,$data_arr);
                    if($re['errcode'] != 0){
                        return $this->error("错误代码".$re['errcode']);
                    }
                }
                // 提交事务
                Db::commit();
                $this->publish_cancle1(input('order_id'));
                return 1;
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
        }else{
            $this->redirect('index');
        }
    }
//??载客

//到达目的地
    public function down_car(){
        if(input('order_id')){
            Db::startTrans();
            try{
                $status= Db::name('dd_publish_orders')
                    ->where('id',input('order_id'))->find();
                if ($status['status']==2){
                    $this->success('订单已完成','index');
                }
                //更新订单状态
                $data1 = Db::name('dd_publish_orders')
                    ->where('id',input('order_id'))
                    ->update(['status'=>2]);
                //更新发布状态
                $publish = Db::name('dd_publish_orders')->field('publish_id')->where('id',input('order_id'))->find();
                $data2 = Db::name('dd_publish')
                    ->where('id',$publish['publish_id'])
                    ->update(['status'=>2]);
                // 提交事务
                if($data1&&$data2&&$publish){
                    Db::commit();
                    return 1;
                }else{
                    return 0;
                }

            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
        }else{
            $this->redirect('index');
        }


    }

    public function down_car_ok(){
        $status= Db::name('dd_publish_orders')
            ->where('id',input('order_id'))->find();
        if ($status['status']==2){
            $this->success('订单已完成');
        }else{
            $this->error();
        }
    }

    public function up_car1(){

        // 参数验证
        if(input('order_id')){

            // 获取订单信息
            $order_info = Db::name('dd_publish_orders')->where('id',input('order_id'))->find();

            // 避免重复支付
            if ($order_info['status']==1||$order_info['status']==2){

                $this->error('已支付，无需重复支付');

            }else{

                $re = Db::name('dd_publish_orders')->where('id',input('order_id'))->update(['status'=>1]);

                $this->success("成功",'',$re);

            }
        }else{
            $this->error('参数有误');
        }
    }


    /**
     * 发起支付
     * URL：index/sunfc.passenger/up_car
     * return   $re        array        微信支付加密信息
     */
    public function up_car(){

        // 参数验证
        if(input('order_id')){

            // 获取订单信息
            $order_info = Db::name('dd_public_orders')->where()->where('id',input('order_id'))->find();

            // 避免重复支付
            if ($order_info['status']==2||$order_info['status']==3){

                $this->error('已支付，无需重复支付');

            }else{

                // 获取用户openid
                $openid=Db::name('dd_users')->where('uid',$this->uid)->value('openid');

                // 调用支付接口
                $pay = new Pay();
                $re = $pay->pay($openid,$order_info['order_id'],$order_info['money'],url('pay/wxReturnOrder,["id"=>$order_id]'),'上车支付');

                $this->success("成功",'',$re);

            }
        }else{
            $this->error('参数有误');
        }
    }


    //    发布行程调用接口
    public function publish_ok($publish_id){
        $data_arr= Db::name('dd_users')->where('uid',$this->uid)->field('openid,realname')->find();
        $aa=Db::name('dd_publish')->where('id',$publish_id)->find();
        if ($aa){
            $weixin = new  \app\index\controller\Weixin();
            $openid         =$data_arr['openid'];

            $re=$this->getWxTemplate();
            $res=json_decode($re, true);
            $templete_id    = $res['add_publish'];
            $url            = Request()->domain().'/index/sunfc.passenger/wait_order?publish_id='.$publish_id;
            $data_arr1['user_name']                  = ['value'=>"亲爱的".$data_arr['realname']."，您的行程已发布，系统正努力为你寻找顺路乘客，请耐心等待哟~",'color'=>'#ff8a5a'];
            $data_arr1['begin_place']                = ['value'=>$aa['begin_place'].$aa['begin_detail'],'color'=>'#1491cb'];
            $data_arr1['end_place']                 = ['value'=>$aa['end_place'].$aa['end_detail'],'color'=>'#1491cb'];
            $data_arr1['depart_time']                = ['value'=>date("Y-m-d H:i",$aa['depart_time']),'color'=>'#1491cb'];
            $data_arr1['number']                     = ['value'=>$aa['number'],'color'=>'#1491cb'];
            $data_arr1['content']                    = ['value'=>$aa['content'],'color'=>'#1491cb'];

            $re = $weixin->sentMsg($openid , $templete_id , $url ,$data_arr1);
            if($re['errcode'] != 0){
                return $this->error("错误代码".$re['errcode']);
            }
        }
    }
    //    取消发布行程调用接口
    public function publish_cancle($publish_id){
        $data_arr= Db::name('dd_users')->where('uid',$this->uid)->field('openid,realname')->find();
        $aa=Db::name('dd_publish')->where('id',$publish_id)->find();
        if ($aa){
            $weixin = new  \app\index\controller\Weixin();
            $openid         = $data_arr['openid'];
            $re=$this->getWxTemplate();
            $res=json_decode($re, true);
            $templete_id    = $res['cancel_publish'];
            $url            = Request()->domain().'/index/sunfc.passenger/cancel_success?publish='.$publish_id;

            $data_arr1['user_name']                  = ['value'=>"亲爱的".$data_arr['realname']."，您的行程已取消，期待您的下次派单呦~",'color'=>'#ff8a5a'];
            $data_arr1['begin_place']                = ['value'=>$aa['begin_place'].$aa['begin_detail'],'color'=>'#1491cb'];
            $data_arr1['end_place']                 = ['value'=>$aa['end_place'].$aa['end_detail'],'color'=>'#1491cb'];
            $data_arr1['cancel_time']                = ['value'=>date("Y-m-d H:i:s"),'color'=>'#1491cb'];

            $re = $weixin->sentMsg($openid , $templete_id , $url ,$data_arr1);
            if($re['errcode'] != 0){
                return $this->error("错误代码".$re['errcode']);
            }
        }
    }

    //    接单行程成功接口
    public function publish_ok1($order_id){

    }
    //    接单行程取消接口
    public function publish_cancle1($order_id){

    }


    //    行程记录
    public function travel_records(){
        if(request()->isAjax()){
            $page = input('page')?input('page'):1;
            $trip_data1=Db::name('dd_publish_orders')
                ->join('iw_dd_users','iw_dd_users.uid=iw_dd_publish_orders.driver_id')
                ->join('iw_dd_publish','iw_dd_publish_orders.publish_id=iw_dd_publish.id')
                ->field('iw_dd_users.headimgurl,iw_dd_users.nickname,iw_dd_users.trust_rank,
                iw_dd_users.car_info,iw_dd_users.tel,iw_dd_users.sex,iw_dd_publish_orders.create_time,iw_dd_publish_orders.money,
                iw_dd_publish_orders.number,iw_dd_publish.begin_place,iw_dd_publish_orders.id,
                iw_dd_publish.begin_detail,iw_dd_publish.end_place,iw_dd_publish.end_detail,
                iw_dd_publish.status,iw_dd_publish.region,iw_dd_publish_orders.status')
                ->where('iw_dd_publish_orders.passenger_id',$this->uid)
                ->page($page,4)
                ->select();

            foreach ($trip_data1 as $k=>$v){
                if($v['create_time']>strtotime(date('Y-m-d',strtotime('day')))){
                    $trip_data1[$k]['create_time']="今天".date('H:i',$v['create_time']);
                }
                else{
                    $trip_data1[$k]['create_time']= date('m-d H:i',$v['create_time']);
                }
            }
            return json(['code'=>1,'msg'=>'ok','data'=>$trip_data1]);
        }else{
            return $this->fetch();
        }


    }
    public  function driver_log(){
        if(request()->isAjax()){
            $page = input('page')?input('page'):1;
            $trip_data2=Db::name('dd_publish_orders')
                ->join('iw_dd_users','iw_dd_users.uid=iw_dd_publish_orders.passenger_id')
                ->join('iw_dd_publish','iw_dd_publish_orders.publish_id=iw_dd_publish.id')
                ->field('iw_dd_users.headimgurl,iw_dd_users.nickname,iw_dd_users.trust_rank,
                iw_dd_users.car_info,iw_dd_users.tel,iw_dd_users.sex,iw_dd_publish_orders.create_time,iw_dd_publish_orders.money,
                iw_dd_publish_orders.number,iw_dd_publish.begin_place,iw_dd_publish_orders.id,
                iw_dd_publish.begin_detail,iw_dd_publish.end_place,iw_dd_publish.end_detail,
                iw_dd_publish.status,iw_dd_publish.region,iw_dd_publish_orders.status,iw_dd_publish.lonlat_begin,iw_dd_publish.lonlat_end')
                ->where('iw_dd_publish_orders.driver_id',$this->uid)
                ->page($page,5)
                ->select();


            foreach ($trip_data2 as $k=>$v){
                if($v['create_time']>strtotime(date('Y-m-d',strtotime('day')))){
                    $trip_data2[$k]['create_time']="今天".date('H:i',$v['create_time']);
                }
                else{
                    $trip_data2[$k]['create_time']= date('m-d H:i',$v['create_time']);
                }
            }
            foreach ($trip_data2 as $k=>$v){
                $trip_data2[$k]["money"]=$trip_data2[$k]["number"]*5;
            }
            return json(['code'=>1,'msg'=>'ok','data'=>$trip_data2]);
        }else{
            return $this->redirect('index');
        }
    }
//    成为车主
    public function cat_owner(){
        $uid = $this -> uid;
        if($uid){
            $data = Db::name('dd_users')
                -> where('uid',$uid)
                ->where('type',2)
                ->where('auth_driver',2)
                -> find();
            if($data){
                // 如果存在就直接重定向去发布信息界面
                $this -> redirect('sunfc.driver/index');
            }
            else{
                return  $this -> fetch();
            }
        }
        else{
            return  $this -> fetch();
        }


    }
}